package com.itboye.jmsfhz.app.ui.roof;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.annotations.Until;
import com.itboye.app.mian.activity.ActivityGongGao;
import com.itboye.app.mian.activity.ActivitySiFang;
import com.itboye.app.mian.activity.NewsXiangQingActivity;
import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.EnumUtility;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.ModelNewsBean;
import com.itboye.jmsfhz.app.adapter.NewsDongTaiAdapter;
import com.itboye.jmsfhz.app.adapter.RoofGridViewAdapter;
import com.itboye.jmsfhz.app.adapter.RoofMeeageAdapter;
import com.itboye.jmsfhz.app.adapter.RoofToNewsAdapter;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.data.model.LunBoModel;
import com.itboye.jmsfhz.data.model.TuPianNewsBean;
import com.itboye.jmsfhz.data.presenter.LunBoPresenter;
import com.itboye.jmsfhz.data.presenter.Personnelpresenter;
import com.itboye.jmsfhz.data.presenter.Photoersonnelpresenter;
import com.itboye.jmsfhz.widget.CarouselView;
import com.itboye.jmsfhz.widget.MyGridView;
import com.itboye.jmsfhz.widget.XScrollView;
import com.itboye.senab.photoview.PhotoGridview;
import com.lfh.commentdemo.bean.AppModelNewsBean;
import com.lfh.commentdemo.view.MyListView;

public class TodaySpecialOfferItemFragment extends BaseFragment implements
		Observer {

	private CarouselView topLunbo;
	public static XScrollView xScrollView;
	private List<ImageView> bmps;
	private LunBoPresenter lunBoPresenter;
	private MyListView mylistviewnews, mylistviewmessge;
	private MyGridView mymygridview;
	private RelativeLayout relatigongao, realyoutnews, relayoutSiFang,
			realyoutnewss;
	private AppModelNewsBean bean;
	private ModelNewsBean newsBean;
	private Personnelpresenter personnelpresenter;
	private Photoersonnelpresenter photoersonnelpresenter;
	private NewsDongTaiAdapter adapter;
	private RoofToNewsAdapter newsAdapter;
	private String page_num = 1 + "";
	private TextView tvMessge;

	// List<Ban1> hotList = new ArrayList<HomePageModel.Ban1>();
	RoofMeeageAdapter adapter2;
	RoofGridViewAdapter adapter3;
	String page_size = 3 + "";

	List<TuPianNewsBean> list;

	LunBoModel Beannermodel;
	int lunbowight;

	@Override
	public int initView() {
		return R.layout.activity_todayspecialoffer;
	}

	@Override
	public void onMyActivityCreated() {

		WindowManager wm = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);

		int wight = wm.getDefaultDisplay().getWidth();
		int heiht = wm.getDefaultDisplay().getHeight();
		lunbowight = wight / heiht;
		lunbowight = topLunbo.getWidth();
		lunBoPresenter = new LunBoPresenter(this);
		photoersonnelpresenter = new Photoersonnelpresenter(this);
		list = new ArrayList<TuPianNewsBean>();

		Beannermodel = new LunBoModel();
		Beannermodel.setPage_num(page_num);
		Beannermodel.setPage_size(page_size + "");
		Beannermodel.setPosition(6045);
		lunBoPresenter.getInfo(Beannermodel);
		onNews();
		onHeZuo();
		onGngao();
		onpicture();
		xScrollView.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				xScrollView.scrollTo(0, 0);
			}
		});
		relatigongao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(getActivity(), ActivityGongGao.class);
				startActivity(intent);
			}
		});
		realyoutnews.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						NewsXiangQingActivity.class);
				startActivity(intent);
			}
		});
		relayoutSiFang.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(getActivity(), ActivitySiFang.class);
				startActivity(intent);
			}
		});
		realyoutnewss.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(getActivity(), PhotoGridview.class);
				startActivity(intent);
			}
		});

	}

	public void onGngao() {
		int page_size = 1;
		bean = new AppModelNewsBean();
		personnelpresenter = new Personnelpresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.NOTICE.getValue() + "");
		personnelpresenter.delete(bean);
	}

	public void onNews() {
		int page_size = 5;
		newsBean = new ModelNewsBean();
		photoersonnelpresenter = new Photoersonnelpresenter(this);
		photoersonnelpresenter.onUpdate(EnumUtility.NEWS.getValue() + "",
				page_num, page_size + "");// (newsBean);

		mylistviewnews.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "新闻动态");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ newsBean.getList().get(position).getId());
				intent.putExtra("cate", newsBean.getCate());
				intent.putExtra("id", newsBean.getList().get(position).getId());
				newsBean.setCate(EnumUtility.NEWS.getValue() + "");
				startActivity(intent);
			}
		});

	}

	public void onHeZuo() {
		int page_size = 10;
		bean = new AppModelNewsBean();
		personnelpresenter = new Personnelpresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.HEZUO.getValue() + "");
		personnelpresenter.update(bean);

		mylistviewmessge.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent(getActivity(), WebActivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(position).getId());
				intent.putExtra("title", "四方合作");
				bean.setCate(EnumUtility.HEZUO.getValue() + "");
				startActivity(intent);
			}
		});
	}

	public void onpicture() {
		int page_size = 5;
		bean = new AppModelNewsBean();
		personnelpresenter = new Personnelpresenter(this);
		bean.setPage_num(page_num);
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.PHOTO.getValue() + "");
		personnelpresenter.add(bean);
		mymygridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Intent intent = new Intent(getActivity(), WebActivity.class);
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(position).getId());
				intent.putExtra("title", "图片新闻");
				bean.setCate(EnumUtility.PHOTO.getValue() + "");
				startActivity(intent);
			}
		});
	}

	int position;

	@SuppressWarnings("static-access")
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);

		if (model.getEventType() == lunBoPresenter.BannerSuccessEvent) {
			Beannermodel = (LunBoModel) model.getData();
			bmps = new ArrayList<ImageView>();
			for (int i = 0; i < Beannermodel.getResult().size(); i++) {
				position = i;
				ImageView imageView = new ImageView(getActivity());

				imageView.setScaleType(ScaleType.FIT_XY);
				com.nostra13.universalimageloader.core.ImageLoader
						.getInstance().displayImage(
								GlobalConfig.JIUYE
										+ Beannermodel.getResult().get(i)
												.getImg(), imageView);
//				imageView.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View arg0) {
//						// TODO Auto-generated method stub
//
//						if (Beannermodel.getResult().get(0).getUrl() == null
//								|| Beannermodel.getResult().get(0).getUrl() == "") {
//						} else {
//							Intent intent = new Intent(getActivity(),
//									WebActivity.class);
//							intent.putExtra("url", GlobalConfig.WEBURL
//									+ Beannermodel.getResult().get(0).getUrl());
//							startActivity(intent);
//						}
//
//					}
//				});
				bmps.add(imageView);

			}

			topLunbo.setImageBitmaps(bmps);
		}
		if (model.getEventType() == photoersonnelpresenter.NewsSuEvent) {

			newsBean = (ModelNewsBean) model.getData();
			newsAdapter = new RoofToNewsAdapter(getActivity(), newsBean);
			mylistviewnews.setAdapter(newsAdapter);
			xScrollView.smoothScrollTo(0, 0);
		}
		// 四方合作单位
		if (model.getEventType() == personnelpresenter.NewsSuccessEvent) {

			bean = (AppModelNewsBean) model.getData();
			adapter2 = new RoofMeeageAdapter(getActivity(), bean);
			mylistviewmessge.setAdapter(adapter2);
			xScrollView.smoothScrollTo(0, 0);
		}// 公告
		if (model.getEventType() == personnelpresenter.GonggaoSuccessEvent) {
			bean = (AppModelNewsBean) model.getData();
			tvMessge.setText(bean.getList().get(0).getPost_title());
			xScrollView.smoothScrollTo(0, 0);
		}// 图片
		if (model.getEventType() == personnelpresenter.PictureSuccessEvent) {
			bean = (AppModelNewsBean) model.getData();
			System.out.println("tupan atioe " + bean.getList().size());
			adapter3 = new RoofGridViewAdapter(getActivity(), bean);
			mymygridview.setAdapter(adapter3);
			xScrollView.smoothScrollTo(0, 0);
		}// 轮播

	}
}

// xScrollView.smoothScrollTo(0, 0);
// if (model != null) {
//
// if (model.getEventType() == LunBoPresenter.BannerSuccessEvent) {
// final HomePageModel arrayBanner = (HomePageModel) model
// .getData();
// hotList = arrayBanner.getP_hot();
// bmps = new ArrayList<ImageView>();
// for (int i = 0; i < arrayBanner.getBan1().size(); i++) {
// position = i;
// ImageView imageView = new ImageView(getActivity());
//
// imageView.setScaleType(ScaleType.FIT_XY);
// com.nostra13.universalimageloader.core.ImageLoader
// .getInstance().displayImage(
// GlobalConfig.API_URL
// + "/picture/index?id="
// + arrayBanner.getBan1().get(i)
// .getImg(), imageView);
// bmps.add(imageView);
//
// }
//
// topLunbo.setImageBitmaps(bmps);
// }
// }
